package com.minisoft.utils.excel;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author JueSu
 * @description: TODO
 * @date 2023/7/28 13:00
 */
public class ExportDaasResultBuilding {

    public static Map<Integer, String> propertyTypeMap = new HashMap<>();
    public static Map<Integer, String> buildingTypeMap = new HashMap<>();

    static {
        propertyTypeMap.put(1, "住宅");
        propertyTypeMap.put(2, "酒店式公寓");
        propertyTypeMap.put(3, "住宅式公寓");
        propertyTypeMap.put(4, "别墅");
        propertyTypeMap.put(5, "办公");
        propertyTypeMap.put(6, "商铺");
        propertyTypeMap.put(7, "商业");
        propertyTypeMap.put(8, "养老度假村");

        buildingTypeMap.put(1, "低层住宅");
        buildingTypeMap.put(2, "多层住宅");
        buildingTypeMap.put(3, "小高层住宅");
        buildingTypeMap.put(4, "高层住宅");
        buildingTypeMap.put(5, "商业");
        buildingTypeMap.put(6, "办公");
        buildingTypeMap.put(7, "高层办公");
        buildingTypeMap.put(8, "洋房");
        buildingTypeMap.put(9, "超高层住宅");
        buildingTypeMap.put(10, "平层公寓");
        buildingTypeMap.put(11, "LOFT公寓");
        buildingTypeMap.put(12, "独栋别墅");
        buildingTypeMap.put(13, "联排别墅");
        buildingTypeMap.put(14, "叠拼别墅");
        buildingTypeMap.put(15, "别墅");
        buildingTypeMap.put(16, "合院");
        buildingTypeMap.put(17, "双拼别墅");
        buildingTypeMap.put(18, "超甲级写字楼");
        buildingTypeMap.put(19, "甲级写字楼");
        buildingTypeMap.put(20, "乙级写字楼");
        buildingTypeMap.put(21, "底商");
        buildingTypeMap.put(22, "沿街商业");
        buildingTypeMap.put(23, "地下商业");
        buildingTypeMap.put(24, "车位");
        buildingTypeMap.put(25, "储藏室");
    }
    
    public static void main(String[] args) throws JsonProcessingException {
        String outputFilePath = "D:\\temp/daasResult-"+System.currentTimeMillis()+".xlsx";
        // Read JSON data from the input file
        String jsonData = FileUtil.readString(ExportDaasResultBuilding.class.getResource("/daasResult.txt"),"utf-8");

        // Parse JSON data to get rows and column_list
        JSONObject jsonObject = JSON.parseObject(jsonData);
        List<String> columnList = jsonObject.getJSONArray("column_list").toJavaList(String.class);
        columnList.add("buildingTypeText");
        columnList.add("propertyTypeText");
        List<List<String>> rows = new ObjectMapper().readValue(jsonObject.getJSONArray("rows").toString(), new TypeReference<List<List<String>>>() {});

        for (List<String> row : rows) {
            if (StrUtil.isNotBlank(row.get(6))) {
                row.add(buildingTypeMap.get(Integer.parseInt(row.get(6))));
            } else {
                row.add("");
            }
            if (StrUtil.isNotBlank(row.get(7))) {
                row.add(propertyTypeMap.get(Integer.parseInt(row.get(7))));
            } else {
                row.add("");
            }
        }
        // Create Excel writer
        ExcelWriter writer = ExcelUtil.getWriter(outputFilePath);

        // Set the header row
        writer.writeHeadRow(columnList);

        // Write data rows
        writer.write(rows);

        // Close the writer and save the Excel file
        writer.close();

        System.out.println("done");
    }

}
